package com.diligreat.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.diligreat.action.base.BaseAction;
import com.diligreat.domain.Content;
import com.diligreat.domain.Lang;
import com.diligreat.domain.Selection;
import com.diligreat.service.ContentService;
import com.diligreat.service.IndexService;
import com.diligreat.service.SelectionService;
import com.opensymphony.xwork2.ModelDriven;

public class ContentAction extends BaseAction implements ModelDriven<Content>{
	private Logger logger = LoggerFactory.getLogger(ContentAction.class);
	
	private Content content = new Content();
	private IndexService indexService;
	private ContentService contentService;
	private List<Selection> choosepro;
	private List<Lang> langpro;
	private List<Content> contentList;
	private SelectionService selectionService;
	
	private String pdfName;
	
	private long pageid;
	private Content cw;
	
	private String tip;
	private long action;
	/**
	 * 
	 */
	private static final long serialVersionUID = -5078963174788113217L;
	
	public String consultation()
	{
		return SUCCESS;
	}
	
	public String input() throws Exception {
		//this.choosepro=this.indexService.getAllSelectionByOrder(1);
		//this.langpro=this.indexService.getGroupLang();
		choosepro = selectionService.listAll();
		return INPUT;
	}
	
	public String adminInput(){
		
		return "adminInput";
	}
	public String submit() throws UnsupportedEncodingException{
		content.setUpdatetime(System.currentTimeMillis());
		if(StringUtils.hasText(pdfName)){
			content.setHavePdf(1);
			//重命名pdf文件
			String filePath = ServletActionContext.getServletContext().getRealPath("/PDF/");
			File file = new File(filePath + File.separator + pdfName);
			String newName = content.getTitle() + ".pdf";
			File newFile = new File(filePath + File.separator +  newName);
			boolean flag = file.renameTo(newFile);
			if(flag == false){
				logger.error("重命名文件“{}”为“{}”时失败！", new String[]{pdfName,newName});
			}
		}
		contentService.addContent(content);
		return "operateSucc";
	}
	
	public String coverInput(){
		contentService.addCover(content);
		return "coverInput";
	}
	
	public String info()
	{
		this.cw=this.indexService.getContentById(this.pageid);
		
		return SUCCESS;
	}
	
	
	//增加语言选项
	public String lang()
	{
		this.langpro=this.indexService.getLangResource();
		
		if(this.action==1)
		{
			this.tip="您将提交一个顶级区域,比如CHINA下面将会划分为中文CHINESE版和英文ENGLISH版,您将提交类似于CHINA的这个顶级区域";
		}
		else if(this.action==2)
		{
			this.tip="您将提交一个选项的子分类";
		}
		else if(this.action==3)
		{
			this.tip="您将添加一个子连接";
		}
		else if(this.action==4)
		{
			this.tip="您将修改一个子连接";
			
			this.cw=this.indexService.getContentById(this.pageid);
		}
		
		return SUCCESS;
	}
	
	private String titlelang;
	
	public String langsubmit()
	{
		if(this.action==1)
		{
			this.indexService.insertTopLang(this.titlelang, 0, "");
		}
		else if(this.action==2)
		{
			this.indexService.insertTopLang(this.titlelang, this.pageid, "");
		}
		else if(this.action==3)
		{
			this.content.setTitle(this.titlelang);
			this.content.setLangId(this.pageid);
			this.contentService.addContent(content);
		}
		else if(this.action==4)
		{
		}
		
		return SUCCESS;
	}
	/**
	 * 查询文章列表
	 * @return
	 */
	public String list(){
		contentList = contentService.getAllContent();
		return SUCCESS;
	}
	/**
	 * 文章详细信息
	 * @return
	 */
	public String detail(){
		content = contentService.retriveContent(content.getId());
		return SUCCESS;
	}
	/**
	 * 文章修改
	 * @return
	 */
	public String edit(){
		content = contentService.retriveContent(content.getId());
		choosepro = indexService.getAllSelectionByOrder(1);
		//langpro = indexService.getGroupLang();
		return SUCCESS;
	}
	public String editSubmit(){
		Integer id = contentService.updateContent(content);
		logger.info("ID为{}的文章内容已更新！",id);
		return "operateSucc";
	}
	/**
	 * 文章删除
	 * @return
	 */
	public String del(){
		Integer id = contentService.deleteContent(content.getId());
		logger.info("ID为{}的文章内容已删除！",id);
		return "operateSucc";
	}
	public String success(){
		
		return SUCCESS;
	}
	
	public String fail(){
		
		return FAIL;
	}
	
	public String error(){
		
		return ERROR;
	}
	
	public String article()
	{
		this.cw=this.indexService.getContentById(this.pageid);
		return SUCCESS;
	}
	//------------------------------ GET AND SET ------------------------------
	
	public List<Selection> getChoosepro() {
		return choosepro;
	}

	public void setIndexService(IndexService indexService) {
		this.indexService = indexService;
	}

	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}

	@Override
	public Content getModel() {
		return content;
	}
	public long getPageid() {
		return pageid;
	}

	public void setPageid(long pageid) {
		this.pageid = pageid;
	}

	public Content getContent() {
		return content;
	}

	/*public void setContent(Content content) {
		this.content = content;
	}*/

	public Content getCw() {
		return cw;
	}

	public void setCw(Content cw) {
		this.cw = cw;
	}

	public List<Lang> getLangpro() {
		return langpro;
	}

	public void setLangpro(List<Lang> langpro) {
		this.langpro = langpro;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public long getAction() {
		return action;
	}

	public void setAction(long action) {
		this.action = action;
	}

	public String getTitlelang() {
		return titlelang;
	}

	public void setTitlelang(String titlelang) {
		this.titlelang = titlelang;
	}

	public List<Content> getContentList() {
		return contentList;
	}

	public void setSelectionService(SelectionService selectionService) {
		this.selectionService = selectionService;
	}

	public void setPdfName(String pdfName) {
		this.pdfName = pdfName;
	}



}
